﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FapCore.Listeners;
using System.Linq;
using FAP.DataAccess;
using FAP.DataAccess.BLL;
using FapCore.Forms;


namespace FapCore
{
    public partial class DataShowingForm : Form
    {
        public DataHelper helper { get { return new DataHelper(); } }
        private Point currentPoint;
        private int marginLeft = 10;
        private int marginTop = 0;
        private List<List<Control>> data = new List<List<Control>>();
        List<chitietbuocthuchien> fields = new List<chitietbuocthuchien>();
        private int idbuocthuchien;
        buocthuchien bth;

        public string Link
        {
            get { return txtLink.Text;}
            set { txtLink.Text = value; }
        }

        public DataShowingForm(List<chitietbuocthuchien> Fields, buocthuchien Bth)
        {
            this.pData = new Panel();
            this.fields = Fields;
            this.bth = Bth;
            InitializeComponent();
            createHeader();
            addControl(Fields);
        }

        //To create Header
        private void createHeader()
        {
            currentPoint = new Point(lbVerify.Location.X, lbVerify.Location.Y + marginTop + lbVerify.Height);

            Label lbFieldName = new Label();
            lbFieldName.Text = "Field Name";
            lbFieldName.ForeColor = Color.Blue;
            lbFieldName.Location = currentPoint;
            pData.Controls.Add(lbFieldName);

            Label lbDataType = new Label();
            lbDataType.Text = "Value";
            lbDataType.ForeColor = Color.Blue;
            lbDataType.Location = new Point(lbFieldName.Location.X + lbFieldName.Width + marginLeft, currentPoint.Y);
            pData.Controls.Add(lbDataType);

            Label lbFieldType = new Label();
            lbFieldType.Text = "Field Type";
            lbFieldType.ForeColor = Color.Blue;
            lbFieldType.Location = new Point(lbDataType.Location.X + lbDataType.Width + 70, currentPoint.Y);
            pData.Controls.Add(lbFieldType);
            currentPoint.Y = lbFieldName.Location.Y + marginTop + lbFieldName.Height;
        }

        public void addControl(List<chitietbuocthuchien> fields)
        {
            foreach (chitietbuocthuchien f in fields)
            {
                pData.MouseDoubleClick += new MouseEventHandler(pData_MouseDoubleClick);
                f.idbuocthuchien = idbuocthuchien;
                f.idbuocthuchien = bth.id;
                Label lbName = new Label();
                lbName.Text = f.tenfield;
                lbName.AutoSize = true;
                lbName.Location = currentPoint;
                currentPoint.X += marginLeft + lbName.Width;//increase X                

                TextBox txtName = new TextBox();
                txtName.Text = f.dulieumacdinh;
                txtName.Location = currentPoint;
                txtName.Width = 150;
                txtName.ReadOnly = true;
                currentPoint.X += marginLeft + txtName.Width;
                txtName.MouseDoubleClick += new MouseEventHandler(txtName_MouseDoubleClick);

                ComboBox cbbDataType = new ComboBox();
                cbbDataType.Items.AddRange(new String[] { "Number", "String" });
                cbbDataType.Location = currentPoint;
                cbbDataType.DropDownStyle = ComboBoxStyle.DropDownList;
                currentPoint.X += cbbDataType.Width + marginLeft;
                cbbDataType.SelectedIndex = 1;

                GroupBox gbFieldFormat = new GroupBox();
                gbFieldFormat.Text = "Choose FieldFormat";
                //gbCheckBox.AutoSize = true;
                gbFieldFormat.Size = new System.Drawing.Size(200, 50);
                gbFieldFormat.Location = currentPoint;

                RadioButton rIsUnique = new RadioButton();
                rIsUnique.Text = "Unique";
                rIsUnique.Width = 70;
                rIsUnique.Location = new Point(10, 20);

                RadioButton rIsRunTime = new RadioButton();
                rIsRunTime.Text = "Run Time";
                rIsRunTime.Width = 50;
                rIsRunTime.Location = new Point(80, 20);

                RadioButton rIsDefault = new RadioButton();
                rIsDefault.Text = "Default";
                rIsDefault.Width = 70;
                rIsDefault.Location = new Point(130, 20);
                rIsDefault.Checked = true;

                List<fieldformat> lst = helper.GetAllFieldFormat();

                ComboBox cbb = new ComboBox();
                cbb.Name = "cbbFieldFormat";
                cbb.DataSource = lst;
                cbb.DropDownStyle = ComboBoxStyle.DropDownList;
                cbb.DisplayMember = "ten";
                cbb.ValueMember = "id";

                //add checkbox to groupbox
                gbFieldFormat.Controls.Add(cbb);
                
                //gbFieldFormat.Controls.Add(rIsRunTime);
                //gbFieldFormat.Controls.Add(rIsDefault);

                //add controls to panel
                pData.Controls.Add(lbName);
                pData.Controls.Add(txtName);
                pData.Controls.Add(cbbDataType);
                pData.Controls.Add(gbFieldFormat);

                //Add controls to list
                List<Control> controls = new List<Control>();
                controls.Add(cbbDataType);
                controls.Add(cbb);
                data.Add(controls);
                currentPoint.Y = gbFieldFormat.Location.Y + gbFieldFormat.Height + marginTop;//increase Y                
                currentPoint.X = lbName.Location.X;

            }
            Button btn = new Button();
            btn.Click += new EventHandler(btn_Click);
            btn.Text = "Submit";
            btn.Location = currentPoint;


            Button btnSave = new Button();
            btnSave.Click += new EventHandler(btnSave_Click);
            btnSave.Text = "Save User";
            currentPoint.X += 150;
            btnSave.Location = currentPoint;
            pData.Controls.Add(btnSave);
            pData.Controls.Add(btn);
            if (btn.Location.X > pData.Location.X ||
                     btn.Location.Y > pData.Location.Y)
            {
                pData.AutoScroll = true;
                /* If the AutoScrollMargin is set to less 
                   than (5,5), set it to 5,5. */
                if (pData.AutoScrollMargin.Width < 5 ||
                   pData.AutoScrollMargin.Height < 5)
                {
                    pData.SetAutoScrollMargin(5, 5);
                }
            }
            this.Size = Screen.PrimaryScreen.WorkingArea.Size;
            this.pData.Size = new Size(this.Width - 100, this.Height - 100);
        }

        void btnSave_Click(object sender, EventArgs e)
        {

            //helper.ThemTaiKhoan(tk);

            //MessageBox.Show("Saved ok!");
        }

        void txtName_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DialogResult res = MessageBox.Show("1. Yes = User name  2. No = Password  3. Cancel", "Choose 1", MessageBoxButtons.YesNoCancel);
            if (res == DialogResult.Yes)
            {
                Account.username = ((TextBox)sender).Text;
            }
            else if (res == DialogResult.No)
            {
                Account.password = ((TextBox)sender).Text;
            }
        }



        void btn_Click(object sender, EventArgs e)
        {
            ChangeLink frmChangeLink = new ChangeLink();
            frmChangeLink.Link = bth.link;
            if (DialogResult.OK == frmChangeLink.ShowDialog())
            {
                bth.link = frmChangeLink.Link;
            }

            idbuocthuchien = helper.ThemBuocThucHien(bth);

            if (idbuocthuchien == -1)
            {
                MessageBox.Show("Fail to load this step, please go back to the previous step!!!");
                this.Close();
            }

            updateData();
            try
            {
                helper.XoaCTBDK(idbuocthuchien);
                foreach (chitietbuocthuchien f in fields)
                {
                    f.idbuocthuchien = idbuocthuchien;
                    helper.ThemCTBDK(f);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.Message+" : "+ex.StackTrace);
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            fields = new List<chitietbuocthuchien>();
            this.Close();


        }

        private void updateData()
        {
            int i = 0;
            for (i = 0; i < data.Count; i++)
            {
                ComboBox cbb = new ComboBox();
                GroupBox gb = new GroupBox();

                Control c = data[i][0];
                if (c is ComboBox)
                {
                    cbb = (ComboBox)c;
                    fields[i].kieudulieu = cbb.Text;
                }

                fieldformat fieldFormat = new fieldformat();

                int fieldFormatId = Int32.Parse(((ComboBox)data[i][1]).SelectedValue.ToString());
                fields[i].fieldformatid = fieldFormatId;
                //fieldFormat = helper.GetFieldFormatId(fieldFormatId);

                //set format for field i
                //fields[i].fieldformat = fieldFormat;

            }
            //fields[data.Count].fieldformat = helper.GetFieldFormatId(4);
        }

        private void pData_Enter(object sender, EventArgs e)
        {

        }

        private void DataShowingForm_Load(object sender, EventArgs e)
        {

        }

        private void pData_Paint(object sender, PaintEventArgs e)
        {

        }

        private void DataShowingForm_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(GetChildAtPoint(e.Location).Text);
        }

        private void DataShowingForm_MouseUp(object sender, MouseEventArgs e)
        {

        }

        void pData_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }
    }
}
